<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>服务管理</el-breadcrumb-item>
      <el-breadcrumb-item>服务申请</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-form :model="addForm" :rules="rules" ref="addFormRef" label-width="100px">
        <el-form-item label="社区选择" prop="community">
          <el-select v-model="addForm.community" placeholder="请选择所在社区">
            <el-option v-for="item in cityData" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="orderAddress">
          <el-input type="textarea" v-model="addForm.orderAddress" :rows="6"></el-input>
        </el-form-item>
        <el-form-item label="服务类型" prop="orderType">
          <el-select v-model="addForm.orderType" placeholder="请选择服务类型" >
            <el-option v-for="item in Type" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="服务描述" prop="orderDes">
          <el-input type="textarea" v-model="addForm.orderDes" :rows="6"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <el-upload name="file" :limit="3" action="http://localhost:8090/order/uploadPicture" :on-preview="handlePreview" :on-remove="handleRemove" list-type="picture" :headers="headersObj" :on-success="handleSuccess">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过超过一张</div>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button @click="resetAddForm">取 消</el-button>
          <el-button type="primary" @click="addOrder">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 图片预览 -->
    <el-dialog title="图片预览" :visible.sync="previewVisible" width="30%">
      <img :src="previewPath" style="width:100%">
    </el-dialog>
  </div>
</template>

<script>
import cityData from '../../assets/js/cityData.js'
export default {
  data () {
    return {
      addForm: {
        orderAddress: '',
        orderType: '',
        orderDes: '',
        attrs: [],
        // residentId: window.getItem('userId')
        residentId: window.sessionStorage.getItem('userId'),
        pics: '',
        community: '',
        Address: ''
      },
      orderType: '',
      rules: {
        orderAddress: [{ required: true, message: '请输入详细地址', trigger: 'blur' }],
        community: [{ required: true, message: '请选择社区', trigger: 'blur' }],
        orderType: [{ required: true, message: '请选择服务类型', trigger: 'blur' }],
        orderDes: [{ required: true, message: '请输入服务内容', trigger: 'blur' }]
      },
      cityData,
      Type: [
        {
          value: 0,
          label: '政务服务'
        },
        {
          value: 1,
          label: '社区服务'
        },
        {
          value: 2,
          label: '其他'
        }
      ],
      // 图片上传请求头对象
      headersObj: {
        Authorization: window.sessionStorage.getItem('token')
      },
      previewPath: '',
      previewVisible: false
    }
  },
  created () {},
  methods: {
    // 图片移除
    handleRemove (file) {
      const filePath = file.response
      const i = this.addForm.attrs.findIndex(item => item.pic === filePath)
      this.addForm.attrs.splice(i, 1)
    },
    // 上传成功
    handleSuccess (response) {
      // const picInfo = { pic: response.data.tmp_path }
      this.addForm.attrs.push(response)
    },
    // 图片预览
    handlePreview (file) {
      // console.log(file)
      this.previewPath = file.url
      this.previewVisible = true
    },
    resetAddForm () {
      this.$refs.addFormRef.resetFields()
    },
    async addOrder () {
      this.$refs.addFormRef.validate(async valid => {
        this.addForm.pics = this.addForm.attrs.join(' ')
        const { data: res } = await this.$http.post('order/addOrder', this.addForm)
        if (res.meta.status !== 200) {
          this.$message.error('添加失败')
          return this.resetAddForm()
        }
        this.$message.success('添加成功')
        this.$router.push('/myOrder')
      })
    }
  }
}
</script>

<style lang="less" scoped>
</style>
